    /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.click.app.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.*;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

/**
 *
 * @author arnold
 */
@Entity
@Table(name = "products")
public class Product extends BaseObject implements Serializable {

    private Long id;
    private String name;
    private String nameEs;
    private String code;
    private String description;
    private String descriptionEs;
    private List<FeatureProduct> listFeatureProducts;
    private ProductType productType;
    private StorageProduct storageProduct;
    private byte[] imageProduct;

    public Product() {
    }

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Column(length = 100)
    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    @Column(length = 255)
    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    @Column(name ="description_es" , length = 255)
    public String getDescriptionEs() {
        return descriptionEs;
    }

    @Transient
    public String getDescriptionLang() {
        if(getLanguaje()!= null && getLanguaje().equals("es") && getDescriptionEs()!= null){
	    return getDescriptionEs();
	}
        return getDescription();
    }

    public void setDescriptionEs(String descriptionEs) {
        this.descriptionEs = descriptionEs;
    }

    @Column(length = 100)
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Column(name="name_es" , length = 100)
    public String getNameEs() {
        return nameEs;
    }

    public void setNameEs(String nameEs) {
        this.nameEs = nameEs;
    }

    @Transient
    public String getNameLang(){
	if(getLanguaje()!= null && getLanguaje().equals("es") && getNameEs()!= null){
	    return getNameEs();
	}
        return getName();
    }

    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "product_featureproduct", joinColumns = {@JoinColumn(name = "product_id")}, inverseJoinColumns = @JoinColumn(name = "feature_id"))
    public List<FeatureProduct> getListFeatureProducts() {
        if (listFeatureProducts == null) {
            listFeatureProducts = new ArrayList<FeatureProduct>();
        }
        return listFeatureProducts;
    }

    public void setListFeatureProducts(List<FeatureProduct> listFeatureProducts) {
        this.listFeatureProducts = listFeatureProducts;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "id_product_type")
    public ProductType getProductType() {
        return productType;
    }

    public void setProductType(ProductType productType) {
        this.productType = productType;
    }

    @Override
    public String toString() {
        ToStringBuilder sb = new ToStringBuilder(this, ToStringStyle.DEFAULT_STYLE).append("id", this.id).append("name", this.name).append("code", this.code).append("description", this.description);
        return sb.toString();
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Product other = (Product) obj;
        if (this.id != other.id && (this.id == null || !this.id.equals(other.id))) {
            return false;
        }
        if ((this.name == null) ? (other.name != null) : !this.name.equals(other.name)) {
            return false;
        }

        if ((this.code == null) ? (other.code != null) : !this.code.equals(other.code)) {
            return false;
        }
        if ((this.description == null) ? (other.description != null) : !this.description.equals(other.description)) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 3;
        hash = 29 * hash + (this.id != null ? this.id.hashCode() : 0);
        hash = 29 * hash + (this.name != null ? this.name.hashCode() : 0);
        hash = 29 * hash + (this.code != null ? this.code.hashCode() : 0);
        hash = 29 * hash + (this.description != null ? this.description.hashCode() : 0);
        return hash;
    }

    @OneToOne(cascade = CascadeType.REMOVE, mappedBy = "product")
    public StorageProduct getStorageProduct() {
        return storageProduct;
    }

    /**
     * @param storageProduct the storageProduct to set
     */
    public void setStorageProduct(StorageProduct storageProduct) {
        this.storageProduct = storageProduct;
    }

   // @Lob
    @Column(name = "image_product", nullable = true)
    public byte[] getImageProduct() {
        return imageProduct;
    }

    public void setImageProduct(byte[] imageProduct) {
        this.imageProduct = imageProduct;
    }
}
